﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:PropertyTools.Wpf">

    <local:NullToBoolConverter x:Key="NullToBoolConverter" />
    <local:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
    <local:DoubleToGridLengthConverter x:Key="DoubleToGridLengthConverter" />

    <SolidColorBrush x:Key="ErrorBrush" Color="Red" />
    <SolidColorBrush x:Key="WarningBrush" Color="Orange" />

    <DataTemplate x:Key="TabHeaderTemplate" DataType="{x:Type local:TabViewModel}">
        <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Icon}" Visibility="{Binding IconVisibility}" Margin="0 0 4 0" Width="12" Height="12" VerticalAlignment="Center" />
            <TextBlock x:Name="header1" Text="{Binding Header}" />
        </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding HasWarnings}" Value="True">
                <Setter TargetName="header1" Property="Foreground" Value="{StaticResource WarningBrush}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding HasErrors}" Value="True">
                <Setter TargetName="header1" Property="Foreground" Value="{StaticResource ErrorBrush}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

    <!-- The DataTemplate for the tabcontrol's ContentTemplate contains a vertical stackpanel inside a scrollviewer-->
    <DataTemplate x:Key="TabPageTemplate">
        <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" Focusable="False">
            <Grid>
                <ItemsControl ItemsSource="{Binding Categories}" Focusable="False" ItemTemplateSelector="{Binding CategoryTemplateSelector, Mode=OneTime}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" IsItemsHost="True" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="{Binding LabelWidth, Converter={StaticResource DoubleToGridLengthConverter}, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" />
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <GridSplitter 
                        Grid.Column="1"
                        HorizontalAlignment="Left"
                        Width="8" Background="Transparent" Focusable="False" ResizeDirection="Columns" />
                </Grid>
            </Grid>
        </ScrollViewer>
    </DataTemplate>

    <!-- GroupBox for the categories -->
    <DataTemplate x:Key="CategoryGroupBoxTemplate" DataType="{x:Type local:CategoryViewModel}">
        <GroupBox Header="{Binding Header}" ToolTip="{Binding ToolTip, Mode=OneTime}" Focusable="False" Margin="2 2 2 8" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <ItemsControl ItemsSource="{Binding Properties}" Focusable="False">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </GroupBox>
    </DataTemplate>

    <!-- Expander for the categories -->
    <DataTemplate x:Key="CategoryExpanderTemplate" DataType="{x:Type local:CategoryViewModel}">
        <Expander Header="{Binding Header}" ToolTip="{Binding ToolTip, Mode=OneTime}" Focusable="False" Margin="2 2 2 8" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <ItemsControl ItemsSource="{Binding Properties}" Focusable="False">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Expander>
    </DataTemplate>

    <!-- Header only for the categories -->
    <DataTemplate x:Key="CategoryHeaderTemplate" DataType="{x:Type local:CategoryViewModel}">
        <StackPanel ToolTip="{Binding ToolTip, Mode=OneTime}" Focusable="False" Margin="2 2 2 8" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <Label Content="{Binding Header}" FontWeight="Bold" />
            <ItemsControl ItemsSource="{Binding Properties}" Focusable="False">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="ErrorTemplate">
        <Rectangle Width="12" Height="12" Fill="{StaticResource ErrorBrush}" VerticalAlignment="Center" Margin="4 0 4 0" />
    </DataTemplate>


    <!-- Default Template for property viewmodels -->
    <DataTemplate DataType="{x:Type local:PropertyViewModel}">
        <DockPanel Margin="0 2 0 2" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <!--
            === Showing errors/warnings next to input fields ===
            <Grid MinWidth="{Binding LabelWidth, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}">
				<Grid.ColumnDefinitions>
					<ColumnDefinition />
					<ColumnDefinition Width="Auto" />
					<ColumnDefinition Width="Auto" />
				</Grid.ColumnDefinitions>
				<Label Content="{Binding Header, Mode=OneTime}" ToolTip="{Binding ToolTip, Mode=OneTime}" HorizontalContentAlignment="{Binding LabelAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" />

				<ContentControl Grid.Column="1" Content="{Binding PropertyWarning}" Margin="4 0 4 0" Focusable="False" Visibility="{Binding PropertyWarning, Converter={StaticResource NullToVisibilityConverter}}" ContentTemplate="{Binding WarningTemplate, Mode=OneTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" />
				<ContentControl Grid.Column="2" Content="{Binding PropertyError}" Margin="4 0 4 0" Focusable="False" Visibility="{Binding PropertyError, Converter={StaticResource NullToVisibilityConverter}}" ContentTemplate="{Binding ErrorTemplate, Mode=OneTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" />
			</Grid>-->

            <Label MinWidth="{Binding LabelWidth,RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" Content="{Binding Header, Mode=OneTime}" ToolTip="{Binding ToolTip, Mode=OneTime}" HorizontalContentAlignment="{Binding LabelAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" />

            <Border x:Name="border1" BorderThickness="{Binding ErrorBorderThickness, Mode=OneTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}">
                <ContentControl x:Name="content1" Content="{Binding}" ToolTip="{Binding ToolTip, Mode=OneTime}" Focusable="False" ContentTemplateSelector="{Binding PropertyTemplateSelector, Mode=OneTime}" />
            </Border>
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding PropertyWarning, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource WarningBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyWarning}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding PropertyError, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource ErrorBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyError}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

    <!--<Grid Margin="0 2 0 2" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="{Binding LabelWidth, Mode=TwoWay,RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label 
                Content="{Binding Header, Mode=OneTime}" 
                ToolTip="{Binding ToolTip, Mode=OneTime}"
                HorizontalContentAlignment="{Binding LabelAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}"/>
            <ContentControl Grid.Column="1"
                Content="{Binding}"
                Focusable="False"
                ContentTemplateSelector="{Binding PropertyTemplateSelector, Mode=OneTime}"/>
            <GridSplitter VerticalAlignment="Stretch" HorizontalAlignment="Right" Width="4"/>
        </Grid>-->

    <!-- Template for a wide property -->
    <DataTemplate DataType="{x:Type local:WidePropertyViewModel}">
        <DockPanel Margin="0 2 0 2" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <Label DockPanel.Dock="Top" Content="{Binding Header, Mode=OneTime}" ToolTip="{Binding ToolTip, Mode=OneTime}" Visibility="{Binding HeaderVisibility, Mode=OneTime}" HorizontalAlignment="{Binding RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}, Path=LabelAlignment}" />
            <Border x:Name="border1" BorderThickness="{Binding ErrorBorderThickness, Mode=OneTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}">
                <ContentControl x:Name="content1" Content="{Binding}" ToolTip="{Binding ToolTip, Mode=OneTime}" Focusable="False" ContentTemplateSelector="{Binding PropertyTemplateSelector, Mode=OneTime}" />
            </Border>
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding PropertyWarning, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource WarningBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyWarning}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding PropertyError, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource ErrorBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyError}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

    <DataTemplate DataType="{x:Type local:CheckBoxPropertyViewModel}">
        <DockPanel Margin="0 2 0 2" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <Border x:Name="border1" BorderThickness="{Binding ErrorBorderThickness, Mode=OneTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}">
                <CheckBox x:Name="content1" Content="{Binding Header, Mode=OneTime}" ToolTip="{Binding ToolTip, Mode=OneTime}" Margin="5 4 5 0" IsChecked="{Binding Value}" IsEnabled="{Binding IsWriteable}" VerticalAlignment="Center" />
            </Border>
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding PropertyWarning, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource WarningBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyWarning}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding PropertyError, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource ErrorBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyError}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>


    <!-- Template for an optional property -->
    <DataTemplate DataType="{x:Type local:OptionalPropertyViewModel}">
        <DockPanel Margin="0 2 0 2" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <Grid MinWidth="{Binding LabelWidth,RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}">
                <CheckBox Content="{Binding Header, Mode=OneTime}" ToolTip="{Binding ToolTip, Mode=OneTime}" Margin="5 0 5 0" HorizontalAlignment="{Binding LabelAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" IsChecked="{Binding IsOptionalChecked}" VerticalAlignment="Center" />
            </Grid>

            <Border x:Name="border1" BorderThickness="{Binding ErrorBorderThickness, Mode=OneTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}">
                <ContentControl x:Name="content1" Content="{Binding}" ToolTip="{Binding ToolTip, Mode=OneTime}" Focusable="False" ContentTemplateSelector="{Binding PropertyTemplateSelector, Mode=OneTime}" IsEnabled="{Binding IsOptionalChecked}" />
            </Border>
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding PropertyWarning, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource WarningBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyWarning}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding PropertyError, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource ErrorBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyError}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

    <!-- Template for the resettable property -->
    <DataTemplate DataType="{x:Type local:ResettablePropertyViewModel}">
        <DockPanel Margin="0 2 0 2" IsEnabled="{Binding IsEnabled}" Visibility="{Binding Visibility}">
            <Label 
                DockPanel.Dock="Left"
                MinWidth="{Binding LabelWidth, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" 
                Content="{Binding Header, Mode=OneTime}" 
                ToolTip="{Binding ToolTip, Mode=OneTime}" 
                HorizontalContentAlignment="{Binding LabelAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}" />
            <Button 
                Name="ResetButton"
                DockPanel.Dock="Right"
                Padding="4 0 4 0"
                Content="{Binding Label, Mode=OneTime}" 
                Command="{Binding ResetCommand, Mode=OneTime}" 
                ToolTip="{Binding ToolTip, Mode=OneTime}" 
                VerticalAlignment="Center" />

            <Border 
                x:Name="border1" 
                BorderThickness="{Binding ErrorBorderThickness, Mode=OneTime, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type local:PropertyEditor}}}">
                <ContentControl 
                    x:Name="content1" 
                    Content="{Binding}" 
                    ToolTip="{Binding ToolTip, Mode=OneTime}" 
                    Focusable="False" 
                    ContentTemplateSelector="{Binding PropertyTemplateSelector, Mode=OneTime}" />
            </Border>
        </DockPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding PropertyWarning, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource WarningBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyWarning}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding PropertyError, Converter={StaticResource NullToBoolConverter}}" Value="False">
                <Setter TargetName="border1" Property="BorderBrush" Value="{StaticResource ErrorBrush}" />
                <Setter TargetName="content1" Property="ToolTip" Value="{Binding PropertyError}" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

</ResourceDictionary>